package com.blunka.framework.client;


import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.UIManager;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.basic.BasicPopupMenuUI;

import com.blunka.framework.client.constants.ImageConstant;
import com.blunka.framework.client.ui.entity.BlkTableMetaData;

import net.sf.json.JSONObject;

/**
 * 桌面托盘
 * @author Administrator
 *
 */
public class BlkClient2/* extends TrayIcon*/{

	private JDialog dialog;
	
	public BlkClient2() {
	//public BlkClient2(Image image,String ps,JPopupMenu Jmenu) {
		/*super(image,ps);

	    //初始化JDialog
	    dialog = new JDialog();
	    dialog.setUndecorated(true);//取消窗体装饰
	    dialog.setAlwaysOnTop(true);//设置窗体始终位于上方
	        
	    //设置系统图标大小为自动调整
	    this.setImageAutoSize(true);
	        
	    //为TrayIcon设置鼠标监听器
	    this.addMouseListener(new MouseAdapter() {
	    	
	    	@Override
	        public void mouseReleased(MouseEvent e) {
	            	
	        	//鼠标右键在组件上释放时调用，显示弹出菜单
	            if (e.getButton()==MouseEvent.BUTTON3 && Jmenu != null) {
	            	
	            	//设置dialog的显示位置
	            	Dimension size = Jmenu.getPreferredSize();
	    	        dialog.setLocation(e.getX()-size.width-3, e.getY() - size.height-3);
	    	        dialog.setVisible(true);
	    	        
	    	        //显示弹出菜单Jmenu
	    	        Jmenu.show(dialog.getContentPane(), 0, 0);
	    	    }
	        }
	    });
	    
	    //为弹出菜单添加监听器
	    Jmenu.addPopupMenuListener(new PopupMenuListener() {
	    	
	    	@Override
	    	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {}
	    	
	    	@Override
		    public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
		    	dialog.setVisible(false);
		    }
	    	
	    	@Override
		    public void popupMenuCanceled(PopupMenuEvent e) {
		        dialog.setVisible(false);
		    }
	    });*/
		
	}

	public static void main(String[] args)throws Exception{
		//BlkTableMetaData a = new BlkTableMetaData();
		String str = "{\"is_primary_key\":null,\"is_hidden\":null,\"father_tablemetadata_id\":\"5556360174514239A488012256037E52\",\"show_width\":0,\"user_config\":\"\",\"sel_options\":\"\",\"is_reader\":null,\"is_empty\":null,\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":null,\"model_type\":\"TABLE\",\"describes\":\"\",\"tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"model_field\":null,\"en_us\":\"T_BKFM_KJCS\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS\",\"view_sql\":\"\",\"order_by\":1,\"input_promit\":\"\",\"zh_cn\":\"控件测试\",\"is_viewer\":null,\"model_field_len\":0,\"childArray\":[{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"select\"},\"sel_options\":{\"RangConfig\":{\"configType\":\"1\",\"Config\":[{\"0\":\"下拉1\"},{\"1\":\"下拉2\"}]}},\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"73FF85F905B04AAEA6C3BF35B422101B\",\"model_field\":\"STRING\",\"en_us\":\"XLKJ_CL\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/XLKJ_CL\",\"view_sql\":\"\",\"order_by\":1,\"input_promit\":\"\",\"zh_cn\":\"下拉控件_常量\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"select\"},\"sel_options\":{\"RangConfig\":{\"configType\":\"2\",\"Config\":{\"Sql\":\"SELECT USER_NO AS VALUE,USER_NAME AS DISPLAYVALUE FROM BLK_BASE_USERS\"}}},\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"32EF862EC55B4259A190A72E3E5A1A83\",\"model_field\":\"STRING\",\"en_us\":\"XL_SQL\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/XL_SQL\",\"view_sql\":\"\",\"order_by\":3,\"input_promit\":\"\",\"zh_cn\":\"下拉_SQL\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"select\",\"linkage\":true,\"linkagefield\":\"CITY\"},\"sel_options\":{\"RangConfig\":{\"configType\":\"2\",\"Config\":{\"Sql\":\"SELECT REGION_NO AS VALUE,REGION_NAME AS DISPLAYVALUE FROM BLK_DIC_REGION WHERE REGION_LEVEL='1' ORDER BY ORDER_BY ASC\"}}},\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"AAD6985026A14D4DAD66299275D4A6BB\",\"model_field\":\"STRING\",\"en_us\":\"SF\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/SF\",\"view_sql\":\"\",\"order_by\":5,\"input_promit\":\"\",\"zh_cn\":\"省份\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"select\"},\"sel_options\":{\"RangConfig\":{\"configType\":\"2\",\"Config\":{\"Sql\":\"SELECT REGION_NO AS VALUE,REGION_NAME AS DISPLAYVALUE,FATHER_REGION_NO FATHER_VALUE FROM BLK_DIC_REGION WHERE REGION_LEVEL='2'\"}}},\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"193D579FE641445B84C20296333ADA0A\",\"model_field\":\"STRING\",\"en_us\":\"CITY\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/CITY\",\"view_sql\":\"\",\"order_by\":7,\"input_promit\":\"\",\"zh_cn\":\"市\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"html\",\"colspan\":true},\"sel_options\":\"\",\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"E962E9C61C5E4F5A8F552B4CF8688F6A\",\"model_field\":\"STRING\",\"en_us\":\"BZ\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/BZ\",\"view_sql\":\"\",\"order_by\":9,\"input_promit\":\"\",\"zh_cn\":\"备注\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"colspan\":true},\"sel_options\":\"\",\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"3112E7234E5C4E6D89CC41B5757DBFF0\",\"model_field\":\"STRING\",\"en_us\":\"WB\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/WB\",\"view_sql\":\"\",\"order_by\":11,\"input_promit\":\"\",\"zh_cn\":\"文本\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]},{\"is_primary_key\":\"NO\",\"is_hidden\":\"NO\",\"father_tablemetadata_id\":\"4F0D62788DAA4ED687E87A0DB8F56A58\",\"show_width\":150,\"user_config\":{\"ftype\":\"textarea\",\"colspan\":true},\"sel_options\":\"\",\"is_reader\":\"NO\",\"is_empty\":\"YES\",\"group_title\":\"\",\"model_states\":\"FINISHED\",\"error_msg\":\"\",\"is_disabled\":\"YES\",\"model_type\":\"ATTRIBUTE\",\"describes\":\"\",\"tablemetadata_id\":\"AB251559D69A44E3957FE5F65F41B73C\",\"model_field\":\"STRING\",\"en_us\":\"DWB\",\"obj_path\":\"ROOT/XTCS/T_BKFM_KJCS/DWB\",\"view_sql\":\"\",\"order_by\":13,\"input_promit\":\"\",\"zh_cn\":\"大文本\",\"is_viewer\":\"YES\",\"model_field_len\":100,\"childArray\":[]}]}";
		JSONObject josn = JSONObject.fromObject(str);
		//josn.put("tablemetadata_id", "132");
		//josn.put("fff", "eeeeee");
		BlkTableMetaData s  = (BlkTableMetaData)JSONObject.toBean(josn,BlkTableMetaData.class);
		System.out.println(s.getTablemetadata_id());
		/*//将本机系统外观设置为窗体当前外观
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		//初始化窗体
		JFrame frame=new JFrame("My QQ");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);
		frame.setLocationRelativeTo(null);
		frame.setType(Window.Type.UTILITY);
		
		ImageIcon img=new ImageIcon(ImageConstant.ICON_IMG);
		frame.setIconImage(img.getImage());
		
		//定义弹出菜单
		JPopupMenu Jmenu=new JPopupMenu();
		
		//为JPopupMenu设置UI
		Jmenu.setUI(new BasicPopupMenuUI(){
			@Override
			public void paint(Graphics g, JComponent c){
				super.paint(g, c);
				
				//画弹出菜单左侧的灰色背景
				g.setColor(new Color(236,237,238));
				g.fillRect(0, 0, 25, c.getHeight());
				
				//画弹出菜单右侧的白色背景
				g.setColor(new Color(255,255,255));
				g.fillRect(25, 0, c.getWidth()-25, c.getHeight());
			}
		});
		
		//定义弹出菜单项
		JMenuItem online = new JMenuItem("我在线上",new ImageIcon(ImageConstant.ADD));
		JMenuItem busy = new JMenuItem("忙碌",new ImageIcon(ImageConstant.NEW));
		JMenuItem invisible= new JMenuItem("隐身");
		JMenuItem openmenu = new JMenuItem("打开主面板");
		JMenuItem closemenu = new JMenuItem("退出MyQQ");
		
		//添加弹出菜单项到弹出菜单
		Jmenu.add(online);
		Jmenu.add(busy);
		Jmenu.add(invisible);
		Jmenu.addSeparator();//添加分割线
		Jmenu.add(openmenu);
		Jmenu.add(closemenu);
		
		//得到当前系统托盘
		SystemTray systemtray = SystemTray.getSystemTray();
		
		//创建带指定图像、工具提示和弹出菜单的 MyTrayIcon
		BlkClient2 trayicon=new BlkClient2(img.getImage(),"MyQQ",Jmenu);
		
		//将TrayIcon添加到系统托盘
		try {
			systemtray.add(trayicon); 
		} catch (AWTException e1) {
			e1.printStackTrace();
		}
		
		//设置单击击系统托盘图标显示主窗口
		trayicon.addMouseListener(new MouseAdapter(){ 
			@Override
			public void mouseClicked(MouseEvent e) {
				
				//鼠标左键点击,设置窗体状态，正常显示
				if(e.getButton()==MouseEvent.BUTTON1){
					frame.setExtendedState(JFrame.NORMAL);
					frame.setVisible(true);
				}
			}
		});
		
		//定义ActionListener监听器
		ActionListener MenuListen = new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
					           
				if (e.getActionCommand().equals("退出MyQQ")){
					 
					systemtray.remove(trayicon); 
					System.exit(0);
				}
				else if(e.getActionCommand().equals("打开主面板")){
					frame.setExtendedState(JFrame.NORMAL);
					frame.setVisible(true);
				}
					   
		}};
		
		//为弹出菜单项添加监听器
		openmenu.addActionListener(MenuListen);
		closemenu.addActionListener(MenuListen);
		frame.setVisible(true);*/
	}
}
